package com.googlecode.smartgwtmavenplugin.mojo;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.settings.MavenSettingsBuilder;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.StringUtils;

import com.googlecode.smartgwtmavenplugin.Archive;
import com.googlecode.smartgwtmavenplugin.PluginContextKeyConstants;

/**
 * Downloads a collection of files from https://www.isomorphic.com, using the credentials provided, and stores them at the location
 * specified by the 'workdir.download' property.  Download is skipped for those resources that already exist on the filesystem.  
 * This behavior can be changed with the 'overwrite' property.  e.g., -Doverwrite=true.
 * 
 * @goal download
 * @execute goal=configure
 * @requiresOnline true
 * @requiresProject false
 * @requiresDirectInvocation true
 */
public class DownloadMojo extends AbstractMojo {

	/** 
	 * @component 
	 * @required 
	 * @readonly 
	 */ 
	private MavenSettingsBuilder mavenSettingsBuilder; 
	
	/** @parameter property="url.login" */
	private String loginUrl;
	
	/** @parameter property="url.logout" */
	private String logoutUrl;
	
	/** @parameter property="build" */
	private String build;
	
	/** @parameter property="build.date" */
	private String buildDate;
	
	/** @parameter property="url.build.base" */
	private String buildUrl;
	
	/** @parameter property="isc.username" */
	private String username;
	
	/** @parameter property="isc.password" */
	private String password;
	
	/** @parameter property="workdir.download" */ 
	private File folder;
	
	/** @parameter property="overwrite" default-value="false" */
	private boolean overwrite;

	private List<Archive> downloads = new ArrayList<Archive>();
	private DefaultHttpClient httpclient = new DefaultHttpClient();
	
	/**
	 * 
	 */
	public void execute() throws MojoExecutionException, MojoFailureException {

		Set<Archive> licensed = (Set<Archive>) getPluginContext().get(PluginContextKeyConstants.ARCHIVE_SET);

		for (Archive download : licensed) {
			File file = new File(folder, download.getLocalFileName());
			if (file.exists() && !overwrite) {
				getLog().info("Existing archive found at '" + file.getAbsolutePath() + "'.  Skipping download.");
			} else {
				downloads.add(download);
			}	
		}
		if (downloads.isEmpty()) {
			return;
		}
		try {
			setup();
			login();
			for (Archive file : downloads) {
				download(file);
			}
			logout();
		} finally {
	        httpclient.getConnectionManager().shutdown();
	    }
	}

	private void download(Archive archive) throws MojoExecutionException {
		
		String url = buildUrl.concat(archive.getRelativeUrl(build, buildDate));
		HttpGet httpget = new HttpGet(url);
		HttpResponse response;
		
		try {
			response = httpclient.execute(httpget);
		} catch (Exception e) {
			throw new MojoExecutionException("Error issuing GET request for bundle at '" + url + "'", e);
		}

		HttpEntity entity = response.getEntity();
		String contentType = entity.getContentType().getValue();
		if (! contentType.equals(archive.getContentType())) {
			throw new MojoExecutionException(
					"File not found at '" + url + "'." +
			        "Expected response with ContentType " + archive.getContentType() + ", but got " + contentType + ". " +
				    "Is a build available for the given date?  Are your login credentials correct?  " + 
			        "Are you licensed for the specified download?");
		} 
		
		if(!folder.mkdirs() && !folder.exists()) {
			throw new MojoExecutionException("Could not create specified working directory '" + folder.getAbsolutePath() + "'");
        }
		File file = new File(folder, archive.getLocalFileName());
		FileUtils.deleteQuietly(file);
		getLog().info("Downloading file to '" + file.getAbsolutePath() + "'");
		
		try {
			entity.writeTo(new FileOutputStream(file));
		} catch (Exception e) {
			throw new MojoExecutionException("Error writing file to '" + file.getAbsolutePath() + "'", e);
		}
	}
	
	/**
	 * 
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private void login() throws MojoExecutionException {

		if (username == null || password == null) {
			throw new MojoExecutionException("Missing required parameters 'isc.username' and/or 'isc.password'");
		}
		
		getLog().info("Authenticating to '" + loginUrl + "' with username: '" + username + "'");
		
        HttpPost login = new HttpPost(loginUrl);

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("USERNAME", username));
        nvps.add(new BasicNameValuePair("PASSWORD", password));

    	try {
			login.setEntity(new UrlEncodedFormEntity(nvps));
			HttpResponse response = httpclient.execute(login);
			EntityUtils.consume(response.getEntity());
		} catch (IOException e) {
			throw new MojoExecutionException("Error during POST request for authentication", e);
		}
	}
	
	/**
	 * 
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private void logout() {
        HttpPost logout = new HttpPost(logoutUrl);
        getLog().info("Logging off at '" + logoutUrl + "'");
        try {
	        HttpResponse response = httpclient.execute(logout);
	        EntityUtils.consume(response.getEntity());
		} catch (Exception e) {
			getLog().warn("Error at logout ", e);
		}	
	}
	
	private void setup() throws MojoExecutionException {
		Settings settings = null;
		try {
			settings = mavenSettingsBuilder.buildSettings();
			Proxy proxyConfig = settings.getActiveProxy();
			if (proxyConfig != null && isProxied(proxyConfig) ) {
				if (proxyConfig.getUsername() != null) {
					httpclient.getCredentialsProvider().setCredentials(
						new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()),
						new UsernamePasswordCredentials(proxyConfig.getUsername(), proxyConfig.getPassword()));	
				}
				HttpHost proxy = new HttpHost(proxyConfig.getHost(), proxyConfig.getPort());
				httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
			}
		} catch (Exception e) {
			throw new MojoExecutionException("Error obtaining Maven settings", e);
		}
	}
	
	/**
	 * Adapted from the Site plugin's AbstractDeployMojo.  Refer to 
	 * 
	 * http://maven.apache.org/plugins/maven-site-plugin/xref/org/apache/maven/plugins/site/AbstractDeployMojo.html.
	 */
	private boolean isProxied(Proxy proxyConfig) throws MalformedURLException {
		String host = new URL(buildUrl).getHost();
	    String nonProxyHostsAsString = proxyConfig.getNonProxyHosts();

		for (String nonProxyHost : StringUtils.split(nonProxyHostsAsString, ",;|")) {
			
			if (StringUtils.contains(nonProxyHost, "*")) {
				
				// Handle wildcard at the end, beginning or middle of the nonProxyHost
				final int pos = nonProxyHost.indexOf('*');
				String nonProxyHostPrefix = nonProxyHost.substring(0, pos);
				String nonProxyHostSuffix = nonProxyHost.substring(pos + 1);

				// prefix*
				if (StringUtils.isNotEmpty(nonProxyHostPrefix)
						&& host.startsWith(nonProxyHostPrefix)
						&& StringUtils.isEmpty(nonProxyHostSuffix)) {

					return false;
				}
				
				// *suffix
				if (StringUtils.isEmpty(nonProxyHostPrefix)
						&& StringUtils.isNotEmpty(nonProxyHostSuffix)
						&& host.endsWith(nonProxyHostSuffix)) {
					
					return false;
				}
				
				// prefix*suffix
				if (StringUtils.isNotEmpty(nonProxyHostPrefix)
						&& host.startsWith(nonProxyHostPrefix)
						&& StringUtils.isNotEmpty(nonProxyHostSuffix)
						&& host.endsWith(nonProxyHostSuffix)) {
					
					return false;
				}

			} else if (host.equals(nonProxyHost)) {
				return false;
			}
		}
	    return true;
	}
}